package com.decent.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.decent.constant.Constant;
import com.decent.entity.GetOrderEntity;
import com.decent.entity.Order;
import com.decent.entity.common.Message;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.CharsetUtils;
import org.apache.http.util.EntityUtils;
import org.springframework.util.StringUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @Author decent
 * @Create 2021/3/31 - 13:11
 * @Description
 */
public class HttpUtil {
    //post请求方法
//    public static String post(String strURL, String params) {
//        System.out.println(strURL);
//        System.out.println(params);
//        StringBuilder result = new StringBuilder();
//        BufferedReader reader = null;
//
//        StringBuilder sb = new StringBuilder();
//        BufferedReader in = null;
//        try {
//            URL url = new URL(strURL);// 创建连接
//            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//            connection.setDoOutput(true);
//            connection.setDoInput(true);
//            connection.setUseCaches(false);
//            connection.setInstanceFollowRedirects(true);
//            // 设置请求方式
//            connection.setRequestMethod("POST");
//            // 设置接收数据的格式
//            connection.setRequestProperty("Accept", "application/json");
//            connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
//            connection.connect();
//            if (params != null && !Objects.nonNull(params)) {
//                byte[] writebytes = params.getBytes();
//                // 设置文件长度
//                //   connection.setRequestProperty("Content-Length", String.valueOf(writebytes.length));
//                OutputStream outwritestream = connection.getOutputStream();
//                outwritestream.write(params.getBytes());
//                outwritestream.flush();
//                outwritestream.close();
//            }
//            int responseCode = connection.getResponseCode();
//            InputStream inputStream = null;
//
//
//            if (responseCode == 200) {
//                inputStream = new BufferedInputStream(connection.getInputStream());
//            } else {
//                inputStream = new BufferedInputStream(connection.getErrorStream());
//            }
//
//            in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
//            String line;
//            while ((line = in.readLine()) != null) {
//                result.append(line);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            if (reader != null) {
//                try {
//                    reader.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        return result.toString();
//    }

    public static String tyfhPost(GetOrderEntity entity){
//        GetOrderEntity entity = new GetOrderEntity(Constant.SUP_ID, 1, System.currentTimeMillis() + "");

        CloseableHttpClient httpClient = HttpClients.createDefault();
        String entityStr = null;
        CloseableHttpResponse response = null;
        try {
            HttpPost httpPost = new HttpPost(entity.getUrl());
            List<NameValuePair> list = new LinkedList<>();

            BasicNameValuePair param1 = new BasicNameValuePair("supId", entity.getSupId() + "");
            BasicNameValuePair param2 = new BasicNameValuePair("amount", entity.getAmount() + "");
            BasicNameValuePair param3 = new BasicNameValuePair("timestamp", entity.getTimestamp());
            BasicNameValuePair param4 = new BasicNameValuePair("sign", entity.getSign());
            list.add(param1);
            list.add(param2);
            list.add(param3);
            list.add(param4);
            UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");
            httpPost.setEntity(entityParam);
//            httpPost.addHeader("authorization", "932a5c6c237de1996ac39a9f1cf5a875");
            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
            response = httpClient.execute(httpPost);
            HttpEntity entity1 = response.getEntity();
            entityStr = EntityUtils.toString(entity1, "UTF-8");
        } catch (ClientProtocolException e) {
            System.err.println("Http协议出现问题");
            e.printStackTrace();
        } catch (ParseException e) {
            System.err.println("解析错误");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("IO异常");
            e.printStackTrace();
        } finally {
            // 释放连接
            if (null != response) {
                try {
                    response.close();
                    httpClient.close();
                } catch (IOException e) {
                    System.err.println("释放连接出错");
                    e.printStackTrace();
                }
            }
        }

        // 打印响应内容
        System.out.println(entityStr);
        return entityStr;
    }



//    public static void main(String[] args) {
//        String url = "http://192.168.60.177:8880/exam/getOrder";
//
//        GetOrderEntity entity = new GetOrderEntity(Constant.SUP_ID, 1, System.currentTimeMillis() + "");
//
//        String s = tyfhPost(entity, Constant.GET_ORDER);
//        Message t = JSON.parseObject(s, Message.class);
//
//        String s1 = t.getData().toString();
//        String substring = s1.substring(1, s1.lastIndexOf("]"));
//        Order order = JSON.parseObject(substring, Order.class);
//        System.out.println(order);
//
//    }
}
